﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Store_Manager_SM_Stock_Adjustment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        string order;
        string proname;
        decimal qty;
        string Stockcode;

        string said = DropDownList1.Text;
        string status = Bestlife.ERP.BusinessRules.B_Stock_ADjustment.getstatus(said);
        string branch = Bestlife.ERP.BusinessRules.B_Stock_ADjustment.getbranch(said);


        List<Bestlife.ERP.Entities.E_Stock_Adjustment> listgrn = new List<Bestlife.ERP.Entities.E_Stock_Adjustment>();

        int i = ASPxGridView1.VisibleRowCount;
        Boolean result = false;
        List<Object> itemid1 = ASPxGridView1.GetSelectedFieldValues("Pro_ID");

        List<Object> itemid2 = ASPxGridView1.GetSelectedFieldValues("Qty");

        List<Object> itemid4 = ASPxGridView1.GetSelectedFieldValues("Code");

        string user = Session["username"].ToString();
        if (DropDownList1.Text != "")
        {
            if (DropDownList1.Text == "YES")
            {
                string approval = DropDownList1.Text;
                if (itemid1.Count == 0 || itemid2.Count == 0 || itemid4.Count == 0)
                {
                    ASPxLabel4.Visible = true;
                    ASPxLabel4.Text = "Please select order items ";
                }
                else
                {

                    foreach (object selectitemid1 in itemid1)
                    {
                        proname = selectitemid1.ToString();

                    }
                    foreach (object selectitemid2 in itemid2)
                    {
                        qty = decimal.Parse(selectitemid2.ToString());

                    }

                    foreach (object selectitemid4 in itemid4)
                    {
                        Stockcode = selectitemid4.ToString();

                    }
                    for (int k = 0; k < itemid1.Count; k++)
                    {
                        Bestlife.ERP.Entities.E_Stock_Adjustment grn = new Bestlife.ERP.Entities.E_Stock_Adjustment();
                        grn.Code = itemid4[k].ToString();
                        grn.Proid = itemid1[k].ToString();
                        grn.Qty = decimal.Parse(itemid2[k].ToString());
                        listgrn.Add(grn);

                    }



                    if (status == "Add")
                    {
                        if (branch == "Panadura")
                        {
                            foreach (Bestlife.ERP.Entities.E_Stock_Adjustment listgn in listgrn)
                            {

                                Bestlife.ERP.DataAccess.D_Stock_Adjustment.stockadjustmentaddPanadura(listgn, user);
                                //decimal currentqty=Bestlife.ERP.BusinessRules.B_Stock_ADjustment
                            }
                        }
                        if (branch == "Kandy")
                        {
                            foreach (Bestlife.ERP.Entities.E_Stock_Adjustment listgn in listgrn)
                            {

                                Bestlife.ERP.DataAccess.D_Stock_Adjustment.stockadjustmentaddKandy(listgn, user);
                                //decimal currentqty=Bestlife.ERP.BusinessRules.B_Stock_ADjustment
                            }
                        }
                    }
                    if (status == "Minus")
                    {
                        if (branch == "Panadura")
                        {
                            foreach (Bestlife.ERP.Entities.E_Stock_Adjustment listgn in listgrn)
                            {

                                Bestlife.ERP.DataAccess.D_Stock_Adjustment.stockadjustmentminusPanadura(listgn, user);
                                //decimal currentqty=Bestlife.ERP.BusinessRules.B_Stock_ADjustment
                            }
                        }
                        if (branch == "Kandy")
                        {
                            foreach (Bestlife.ERP.Entities.E_Stock_Adjustment listgn in listgrn)
                            {

                                Bestlife.ERP.DataAccess.D_Stock_Adjustment.stockadjustmentminusKandy(listgn, user);
                                //decimal currentqty=Bestlife.ERP.BusinessRules.B_Stock_ADjustment
                            }
                        }
                    }

                }
            }
        }
    }
}
